package lolxxs.chenyanting.controller;


import lolxxs.chenyanting.entity.Result;
import lolxxs.chenyanting.entity.StatusCode;
import lolxxs.chenyanting.model.Resume;
import lolxxs.chenyanting.service.IResumeService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @since 2022-01-01
 */
@RestController
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    @Qualifier(value = "resumeServiceImpl")
    private IResumeService resumeService;


    @PostMapping("/update")
    public Result update(HttpServletRequest request, @RequestBody Resume resume) {
        Claims claims = (Claims)request.getAttribute("claims");
        if (claims != null) {
            if (claims.getId() != null) {
                if ((Integer)claims.get("identity") <  1) {// 如果非HR访问只能访问自己的数据
                    resume.setUserId(Long.valueOf(claims.getId()));
                }
                resume.setUserId(Long.valueOf(claims.getId())); //HR也无法更新简历
                int update = resumeService.update(resume);
                if (update == 1) {
                    return new Result(false, StatusCode.OK, "提交成功");
                }
                if (update == 2) {
                    return new Result(false, StatusCode.OK, "更新成功");
                }

            }
        }
        return new Result(true, StatusCode.UPDATEERROR, "修改失败");
    }

    @PostMapping("/query")
    public Result query(HttpServletRequest request, @RequestBody Resume resume) {
        Claims claims = (Claims)request.getAttribute("claims");
        if (claims != null) { //&& (Integer)claims.get("identity") >=  1 限制只能HR访问
            if (claims.getId() != null ) {
                if ((Integer)claims.get("identity") <  1) {// 如果非HR访问只能访问自己的数据
                    resume.setUserId(Long.valueOf(claims.getId()));
                } else {
                    resume.setUserId(null);
                }

                Resume query = resumeService.query(resume);
                if (query != null) {
                    return new Result(true, StatusCode.OK, "查询成功",query);
                } else {
                    return new Result(true, StatusCode.NOTFOUND, "没有找到记录");
                }
            }
        }
        return new Result(false, StatusCode.ACCESSERROR, "没有权限");
    }
}
